package com.leetcode.algorithm.y22.m06;

import java.util.HashSet;
import java.util.Set;

/**
 * LCP 03. 机器人大冒险
 * 
 * https://leetcode.cn/problems/programmable-robot/
 * 
 * @author jie.deng
 *
 */
class QuestionLcp03Solution01 {
	
	public boolean robot(String command, int[][] obstacles, int x, int y) {
		Set<Integer> obstacleSet = new HashSet<>();
		for (int i = 0; i < obstacles.length; i++) {
			obstacleSet.add(getIdx(obstacles, obstacles[i][0], obstacles[i][1]));
		}

		int curX = 0;
		int curY = 0;
		int step = 0;
		while (curX <= x && curY <= y) {
			char ch = command.charAt(step % command.length());
			step++;
			if (ch == 'U') {
				curY += 1;
			} else if (ch == 'R') {
				curX += 1;
			} else {
				return false;
			}
			if (obstacleSet.contains(getIdx(obstacles, curX, curY))) {
				return false;
			}
			if (curX == x && curY == y) {
				return true;
			}
		}
		return false;
	}

	private Integer getIdx(int[][] obstacles, int x, int y) {
		return x * obstacles.length + y;
	}

    
}